﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Kugar.Core.ExtMethod;
using Kugar.UI.Web.AdminLTE.Helpers;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace Kugar.UI.Web.AdminLTE.PageControls.PageForms
{
    public class PageForm_MultiFileUploadField: PageForm_SingleInputFieldBase
    {
        private SelectorItem[] _fileUrls = null;
        //private string _name = "";
        private bool _disabled = false;
        private string _uploadUrl = "";
        private int _fileSize = 0;

        public PageForm_MultiFileUploadField(PageForm_Form form, ViewContext viewContext) : base(form, viewContext)
        {
            this.Css("webui-form_multifileuploadfield multifileuploader");
        }

        //public PageForm_MultiFileUploadField Name(string name)
        //{
        //    _name = name;

        //    return this;
        //}

        public PageForm_MultiFileUploadField UploadUrl(string url)
        {
            _uploadUrl = url;

            return this;
        }

        /// <summary>
        /// 可上传的最大大小,单位为KB
        /// </summary>
        /// <param name="size"></param>
        /// <returns></returns>
        public PageForm_MultiFileUploadField MaxFileSize(int size)
        {
            _fileSize = size * 1024;

            return this;
        }

        public PageForm_MultiFileUploadField FileUrls(IEnumerable<SelectorItem> fileUrls)
        {
            _fileUrls = fileUrls.ToArrayEx();

            return this;
        }

        public PageForm_MultiFileUploadField FileUrls<TData>(IEnumerable<TData> lst, Func<TData, string> titleGetter,
            Func<TData, string> srcGetter, Func<TData, bool> disableGetter = null)
        {
            _fileUrls = lst.Select(x => new SelectorItem(titleGetter(x), srcGetter(x), disableGetter?.Invoke(x) ?? false)).ToArrayEx();

            return this;
        }

        public PageForm_MultiFileUploadField Disabled(bool disabled=true)
        {
            _disabled = disabled;

            return this;
        }

        protected override void BeforeRender(TextWriter writer)
        {
            this.Data("name", _name);
            this.Data("uploadurl", _uploadUrl)
                .Data("maxfilesize", _fileSize.ToStringEx())
                .Data("disabled",_disabled.ToStringEx())
                ;

            base.BeforeRender(writer);
        }

        protected override void RenderContent(TextWriter writer)
        {
            writer.WriteLine("<table class=\"table table-bordered table-striped\" webui-ignore lay-ignore style=\"display: table;margin-bottom: 0px;\">");
            writer.WriteLine($"<thead><tr><th width=\"50%\">文件名</th><th width=\"87px\">上传进度</th><th width=\"25%\">");

            if (!_disabled)
            {
                writer.WriteLine($"<button type=\"button\" class=\"btn btn-success btn-xs addfile\" {_disabled.If("disabled")}>选择文件</button>");
            }

            writer.WriteLine("</th></tr></thead>");
            writer.WriteLine("<tbody>");

            if (_fileUrls.HasData())
            {
                foreach (var item in _fileUrls)
                {
                    writer.WriteLine("<tr>");

                    writer.WriteLine($"<td style=\"word-break:break-word;\"><span>{item.Title}</span><input type=\"hidden\" name=\"{_name}\" value=\"{item.Value}\" /></td>");
                    writer.WriteLine("<td></td>");
                    writer.WriteLine("<td>");

                    if (!item.Disabled && !_disabled)
                    {
                        writer.Write("<a class=\"badge bg-red badge-del\">删除</a>");
                    }

                    writer.Write("</td>");
                
                    writer.WriteLine("</tr>");
                }                
            }


            writer.WriteLine("</tbody>");
            writer.WriteLine("</table>");
        }
    }
}
